LL_RNG_Init and LL_RNG_DeInit APIs migration

The LL initialization and de-initialization functions previously available in the stm32tnxx_ll_rng.c files within HAL1 are no longer present in HAL2. In HAL2, all LL C files ( stm32tnxx_ll_rng.c) have been removed, leaving only the LL header files ( stm32tnxx_ll_rng.h) that contain static inline functions.

If users need to initialize or de-initialize RNG using the LL layer, they have two methods:

  • Utilize STM32CubeMX2 Code Generation: Rely on STM32CubeMX2 to generate code with the LL layer for RNG initialization and de-initialization.

  • Replace manually the initialization and de-initialization functionsReplace the LL_RNG_Init and LL_RNG_DeInit functions

    manually with equivalent sequences using the static inline functions provided in the stm32tnxx_ll_rng.h file, tailored to their specific configuration and requirements.

This approach ensures that the initialization and de-initialization sequences are precisely addressing the given use case based on the LL static inline functions provided in the stm32tnxx_ll_rng.h header file. The sequences offer improved footprint optimization compared to the previous LL initialization functions found in stm32tnxx_ll_rng.c.

Related concept: Remove stm32tnxx_ll_ppp.c files

Method 1: Utilize STM32CubeMX2 Code Generation

LL RNG configuration panel

  • Open STM32CubeMX2 RNG configuration panel

  • The LL library is used for RNG initialization, with resource initialization code set to be generated and called within the project.

RNG configuration panel

LL RNG main features configuration

Configure the RNG main features.

RNG main feature configuration

LL RNG additional features configuration

Configure the additional RNG features if needed.

RNG additional feature configuration

LL NVIC configuration

Configure the NVIC in case of an interrupt.

RNG NVIC configuration

Generated RNG initialization sequence

The RNG initialization and de-initialization sequences are generated in the mx_rng_ll_init and mx_rng_ll_deinit functions in mx_rng.c file under $YOUR_PROJECT_NAME$/$YOUR_PROJECT_NAME$_SW/generated/STM32Cube_CodeGen.

STM32CubeMX2 Generated : RNG initialization sequence

Topic

Code Snippet HAL1

Code Snippet HAL2

Initialization and configuration sequence

static void MX_RNG_Init(void)
{

  LL_RCC_SetRNGClockSource(LL_RCC_RNG_CLKSOURCE_HSI48);

  /* Peripheral clock enable */
  LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_RNG);

  LL_RNG_Enable(RNG);
  LL_RNG_EnableClkErrorDetect(RNG);

}
RNG_TypeDef *mx_rng_ll_init(void)
{

  LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_RNG);

  LL_RNG_WRITE_REG(RNG, CR, ((uint32_t)RNG_CERT_NIST_CR_VALUE | (uint32_t)RNG_CR_CONDRST));
  LL_RNG_WRITE_REG(RNG, NSCR, RNG_CERT_NIST_NSCR_VALUE);
  LL_RNG_WRITE_REG(RNG, HTCR, RNG_CERT_NIST_HTCR_VALUE);
  LL_RNG_DisableCondReset(RNG);


  LL_RNG_EnableClkErrorDetect(RNG);

  LL_RCC_SetRNGClockSource(LL_RCC_RNG_CLKSOURCE_HSI48);

  return RNG;
}

Deinitialization sequence

void MX_RNG_DeInit(void)
{
  if (LL_RNG_DeInit(&hrng) != HAL_OK)
  {
    Error_Handler();
  }
}
void mx_rng_ll_deinit(void)
{
  LL_RNG_Disable(RNG);
  LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_RNG);
  LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_RNG);
  LL_AHB2_GRP1_DisableClock(LL_AHB2_GRP1_PERIPH_RNG);

}

Method 2: Replace manually the initialization and de-initialization functions

LL_RNG_Init() API migration

According to the use-case, implement the precise sequence call based on the LL static inline functions provided in the stm32tnxx_ll_rng.h header file.

RNG Mode Comparison between HAL1 and HAL2

HAL1

HAL2

LL_RNG_InitTypeDef rng_InitStruct;

rng_InitStruct.ClockErrorDetection = LL_RNG_CED_ENABLE;
LL_RNG_Init(RNG, &rng_InitStruct);
LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_RNG);

LL_RNG_WRITE_REG(RNG, CR, ((uint32_t)RNG_CERT_NIST_CR_VALUE | (uint32_t)RNG_CR_CONDRST));
LL_RNG_WRITE_REG(RNG, NSCR, RNG_CERT_NIST_NSCR_VALUE);
LL_RNG_WRITE_REG(RNG, HTCR, RNG_CERT_NIST_HTCR_VALUE);
LL_RNG_DisableCondReset(RNG);


LL_RNG_EnableClkErrorDetect(RNG);

LL_RCC_SetRNGClockSource(LL_RCC_RNG_CLKSOURCE_HSI48);

LL_RNG_DeInit() API migration

According to the used RNG, Implement the precise calls to LL_AHB2_GRP1_ForceReset() and LL_AHB2_GRP1_ReleaseReset()

Comparison of HAL1 and HAL2

HAL1

HAL2

LL_RNG_DeInit(const RNG)
LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_RNG);
LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_RNG);